<!--
 * @Author: Dee Dee
 * @Date: 2022-03-31 15:43:55
 * @LastEditors: Dee Dee
 * @LastEditTime: 2023-05-12 09:27:47
 * @FilePath: /培训认证系统20230512/src/views/vadmin/permission/user/profile/resetPwd.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="old_password">
      <el-input v-model="form.old_password" placeholder="请输入旧密码" type="password" />
    </el-form-item>
    <el-form-item label="新密码" prop="new_password">
      <el-input v-model="form.new_password" placeholder="请输入新密码" type="password" />
    </el-form-item>
    <el-form-item label="确认密码" prop="confirm_password">
      <el-input v-model="form.confirm_password" placeholder="请确认密码" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/vadmin/permission/user";

export default {
  props: {
    user: {
      type: Object,
    },
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.form.new_password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      test: "1test",
      form: {
        old_password: undefined,
        new_password: undefined,
        confirm_password: undefined,
      },
      // 表单校验
      rules: {
        old_password: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
        ],
        new_password: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        confirm_password: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate((valid) => {
        this.form.id = this.user.id;
        if (valid) {
          updateUserPwd(this.form).then((response) => {
            this.form = {};
            this.$alert('密码修改成功，请重新登录', '提示', {
              confirmButtonText: '确定',
              callback: () => {
                this.$store.dispatch("LogOut").then(() => {
                  location.href = "/";
                });
              }
            });
          });
        }
      });
    },
    close() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    },
  },
};
</script>
